package com.cw.jeeyt.service.impl.system;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.service.bo.system.SystemNoticeBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.system.SystemNoticeDao;
import com.cw.jeeyt.dao.model.system.SystemNoticeDo;
import com.cw.jeeyt.service.converter.system.SystemNoticeConverter;
import com.cw.jeeyt.service.intf.system.SystemNoticeService;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.common.utils.SequenceTool;

/**
 * 公告通知业务实现类
 *
 * @author chenw
 * @version 1.0.0
 */
@Service
public class SystemNoticeServiceImpl implements SystemNoticeService {

    private static final Logger logger = LoggerFactory.getLogger(SystemNoticeServiceImpl.class);
    @Autowired
    private SystemNoticeDao systemNoticeDao;//公告通知数据库操作接口

    /**
     * 新增SystemNotice
     *
     * @param systemNoticeBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertSystemNotice(SystemNoticeBo systemNoticeBo) throws Exception {

        if (systemNoticeBo == null) {
            logger.error("公告通知信息为空");
            return;
        }
        SystemNoticeDo systemNotice = SystemNoticeConverter.bo2Do(systemNoticeBo);
        systemNotice.setId(SequenceTool.nextId());
        systemNotice.setCreateBy(SessionUtil.getSessionUserId());
        systemNotice.setCreateDate(new Date());
        systemNotice.setUpdateBy(SessionUtil.getSessionUserId());
        systemNotice.setUpdateDate(new Date());
        systemNoticeDao.insertSystemNotice(systemNotice);
    }

    /**
     * 更新SystemNotice信息
     *
     * @param systemNoticeBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSystemNotice(SystemNoticeBo systemNoticeBo) throws Exception {

        if (systemNoticeBo == null) {
            logger.error("公告通知信息为空");
            return;
        }
        SystemNoticeDo systemNotice = SystemNoticeConverter.bo2Do(systemNoticeBo);
        systemNotice.setUpdateBy(SessionUtil.getSessionUserId());
        systemNotice.setUpdateDate(new Date());
        systemNoticeDao.updateSystemNotice(systemNotice);
    }

    /**
     * 删除SystemNotice
     *
     * @param systemNoticeBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteSystemNotice(SystemNoticeBo systemNoticeBo) throws Exception {
        if (systemNoticeBo == null) {
            logger.error("公告通知信息为空");
            return;
        }
        systemNoticeDao.deleteSystemNotice(SystemNoticeConverter.bo2Do(systemNoticeBo));

    }

    /**
     * 分页查询SystemNotice
     *
     * @param systemNoticeBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<SystemNoticeBo> queryByPage(SystemNoticeBo systemNoticeBo, Integer startNum, Integer pageSize)
    throws Exception {
        PageInfo<SystemNoticeDo> pageInfo = systemNoticeDao.queryByPage(SystemNoticeConverter.bo2Do(systemNoticeBo),
            startNum, pageSize);
        return new PageInfo<>(pageInfo, SystemNoticeConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据状态查询公告通知内容
     *
     * @param isPublic 是否发布
     * @return
     * @throws Exception
     */
    @Override
    public List<SystemNoticeBo> querySystemNoticeList(String isPublic) throws Exception {
        return SystemNoticeConverter.doList2BoList(systemNoticeDao.querySystemNoticeList(isPublic));
    }

    /**
     * 根据主键查询SystemNotice信息
     *
     * @param id
     * @return
     */
    @Override
    public SystemNoticeBo getSystemNoticeById(String id) throws Exception {
        return SystemNoticeConverter.do2Bo(systemNoticeDao.getSystemNoticeById(id));
    }
}
